using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Forms;
using System.IO;

namespace ExcelReport
{
    public class MainWindowViewModel : NotificationObject
    {
        public MainWindowViewModel()
        {
            OpenConfigFile = new DelegateCommand(OpenConfig);
            OpenTemplateFile = new DelegateCommand(OpenTemplate);
            Commit = new DelegateCommand(WriteToExcelReport);
        }
        private string _templateFilePath;
        public string TemplateFilePath
        {
            get
            {
                return _templateFilePath;
            }
            set
            {
                if (value != _templateFilePath)
                {
                    _templateFilePath = value;
                    RaisePropertyChanged(() => TemplateFilePath);
                }
            }
        }
        private string _configFilePath;
        public string ConfigFilePath
        {
            get
            {
                return _configFilePath;
            }
            set
            {
                if (value != _configFilePath)
                {
                    _configFilePath = value;
                    RaisePropertyChanged(() => ConfigFilePath);
                }
            }
        }
        public DelegateCommand OpenTemplateFile { get; private set; }
        public DelegateCommand OpenConfigFile { get; private set; }
        public DelegateCommand Commit { get; private set; }
        private void OpenTemplate()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "模板文件(*.xls)|*.xls;*.xlsx";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                TemplateFilePath = openFile.FileName;
            }
        }
        private void OpenConfig()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "配置文件|*.config";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                ConfigFilePath = openFile.FileName;
            }
        }

        private void WriteToExcelReport()
        {
            WriteToExcel report = new WriteToExcel();
            report.TemplateFilePath = this.TemplateFilePath;
            //try
            //{
            XMLHelper helper = new XMLHelper();
            List<FormConfigContent> contentList = helper.LoadConfigs(ConfigFilePath);

            ConfigToContent configToContent = new ConfigToContent();
            List<ExcelContent> excelContents = new List<ExcelContent>();

            foreach (var item in contentList)
            {
                ExcelContent content = new ExcelContent();
                content.SheetName = item.ToSheet;
                content.Cell = configToContent.ToCell(item);
                excelContents.Add(content);
            }
            try
            {
                report.Open();
                report.Write(excelContents);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                report.Dispose();
            }
           
            MessageBox.Show("绑定成功");
            //}
            //catch (System.Runtime.InteropServices.COMException ex)
            //{
            //    var exmessage = ex.Message;
            //    report.Close();
            //    MessageBox.Show(ex.Message, "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            //catch (FileNotFoundException e)
            //{
            //    MessageBox.Show(e.Message);
            //}
        }
    }
}
